package week04;


import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

class Person {

	public String name;
	public int age;
	public boolean gender;

	public Person(String name, int age, boolean gender) {
		super();
		this.name = name;
		this.age = age;
		this.gender = gender;
	}

	
	@Override
	public String toString() {
		return name + "-" + age + "-" + gender;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + (gender ? 1231 : 1237);
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (age != other.age)
			return false;
		if (gender != other.gender)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	
	
}

class Student extends Person {

	String stuNo;
	String clazz;

	public Student(String name, int age, boolean gender, String stuNo, String clazz) {
		super(name, age, gender);
		this.stuNo = stuNo;
		this.clazz = clazz;
	}

	public String toString() {
		return "Student:" + super.toString() + "-" + stuNo + "-" + clazz;
	}

	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((clazz == null) ? 0 : clazz.hashCode());
		result = prime * result + ((stuNo == null) ? 0 : stuNo.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (clazz == null) {
			if (other.clazz != null)
				return false;
		} else if (!clazz.equals(other.clazz))
			return false;
		if (stuNo == null) {
			if (other.stuNo != null)
				return false;
		} else if (!stuNo.equals(other.stuNo))
			return false;
		return true;
	}


}

class Company {
	String name;

	public Company(String name) {
		this.name = name;
	}

	public String toString() {
		return name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (getClass() != obj.getClass())
			return false;
		Company other = (Company) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	
	
}
class Employee extends Person {

	double salary;
	Company company;

	public Employee(String name, int age, boolean gender, double salary, Company company) {
		super(name, age, gender);
		this.salary = salary;
		this.company = company;

	}

	@Override
	public String toString() {
		return "Employee:" + super.toString() + "-" + company.toString() + "-" + salary;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((company == null) ? 0 : company.hashCode());
		long temp;
		temp = Double.doubleToLongBits(salary);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Employee other = (Employee) obj;
		if (company == null) {
			if (other.company != null)
				return false;
		} else if (!company.equals(other.company))
			return false;
		
		DecimalFormat df = new DecimalFormat("#.#");
		String a=df.format(this.salary);
		double a1=Double.parseDouble(a);
		String b=df.format(other.salary);
		double b1=Double.parseDouble(b);
		if (a1!=b1)
			return false;
		return true;
	}

}

public class Main2 {

	public static void myprint(List<Person> personList) {
		Iterator it = personList.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {

		List personList = new ArrayList<Person>();
		Scanner sc = new Scanner(System.in);

		while (true) {
			String str = sc.next();

			if (str.equals("s")) {
				Student s = new Student(sc.next(), sc.nextInt(), sc.nextBoolean(), sc.next(), sc.next());
				personList.add(s);
			}

			else if (str.equals("e")) {
				Company c;
				Employee e = new Employee(sc.next(), sc.nextInt(), sc.nextBoolean(), sc.nextDouble(),
						c = new Company(sc.next()));
				personList.add(e);
			} else
				break;
		}

		Collections.sort(personList, new Comparator<Person>() {
			public int compare(Person p1, Person p2) {
				if (p1.name.compareTo(p2.name) == 0) {
					return new Integer(p1.age).compareTo(new Integer(p2.age));

				} else
					return p1.name.compareTo(p2.name);
			}
		});

		

		String choice = sc.nextLine();
		if (choice.equals("exit"))
			return;
		myprint(personList);

		List stuList = new ArrayList<Student>();
		List empList = new ArrayList<Employee>();

		for (int i = 0; i < personList.size(); i++) {
			int flag = 0;
			if (personList.get(i) instanceof Student) {
				for (int j = stuList.size() - 1; j >= 0; j--) {
					if (personList.get(i).equals(stuList.get(j)))
						flag = 1;
				}
				if (flag != 1)
					stuList.add((Student) personList.get(i));
			} else {
				for (int j = empList.size() - 1; j >= 0; j--) {
					if (personList.get(i).equals(empList.get(j)))
						flag = 1;

				}
				if (flag != 1)
					empList.add((Employee) personList.get(i));
			}
		}

		System.out.println("stuList");
		myprint(stuList);
		System.out.println("empList");
		myprint(empList);

	}

}
